﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SpriteGame.UI;
using SpriteGame.Helpers;

namespace SpriteGame.Scenes
{
    class MainMenu : Scene
    {
        public MainMenu(Game1 _parent)
            : base(_parent)
        {
            IsActive = true;
            PausedPhysics = true;
        }

        UIImageList levelSelector;

        String[] levelNames = new String[] { "MoonScene2", "SpaceMultiplayer", "JapanLevelsp", "NewJapan" };

        public override void BuildHUD()
        {
            base.BuildHUD();

            int width = parent.GraphicsDevice.Viewport.Width;
            int height = parent.GraphicsDevice.Viewport.Height;

            int halfWidth = width / 2;
            int halfHeight = height / 2;

            //UIImage menuBackground = new UIImage(0, 0, width, height, "Textures/UI/MenuBG");
            //Hud.AddChild(menuBackground);

            levelSelector = new UIImageList(halfWidth - 256, halfHeight - 74, 512, 256, "Textures/UI/SelectorMenu_empty", new string[] { "Textures/UI/Level1", "Textures/UI/Level2", "Textures/UI/Level3", "Textures/UI/Level4" });
            Hud.AddChild(levelSelector);

            UIImage titleGraphics = new UIImage(halfWidth - 384, halfHeight - 130, 768, 48, "Textures/UI/Logo");
            Hud.AddChild(titleGraphics);

            UIMainMenuContainer container = new UIMainMenuContainer(halfWidth - 128, halfHeight +192, 256, 400);

            UIButtonToggle singlePlayer = new UIButtonToggle(0, 0, 256, 16, "", "Textures/UI/SP");
            UIButtonToggle multiPlayer = new UIButtonToggle(0, 26, 256, 16,"", "Textures/UI/MP");

            UIButtonToggle levelEdit = new UIButtonToggle(0, 52, 256, 64, "Level Editor");


            #if DEBUG
                levelEdit.IsVisible = true;
            #else
                levelEdit.IsVisible = false;
            #endif

            container.AddChild(singlePlayer);
            container.AddChild(multiPlayer);
            Hud.AddChild(levelEdit);

            Hud.AddChild(container);

            multiPlayer.Clicked += new UIPanel.ClickHandler(multiPlayer_Clicked);
            levelEdit.Clicked += new UIPanel.ClickHandler(levelEdit_Clicked);
            singlePlayer.Clicked +=new UIPanel.ClickHandler(singlePlayer_Clicked);

            //AudioManager.SoundManager.PlaySong("Menu", true);
        }

        void  singlePlayer_Clicked(UIPanel _owner)
        {
            parent.ChangeScene(SceneIndex.SINGLEPLAYER, levelNames[levelSelector.Index]);
        }

        void levelEdit_Clicked(UIPanel _owner)
        {
            parent.ChangeScene(SceneIndex.EDITOR,levelNames[levelSelector.Index]);
        }

        void multiPlayer_Clicked(UIPanel _owner)
        {
            parent.ChangeScene(SceneIndex.MULTIPLAYER, levelNames[levelSelector.Index]);
        }

        public override void Update(GameTime _time)
        {
            if (InputHandler.IsGamepadButtonPressed(Microsoft.Xna.Framework.Input.Buttons.RightShoulder))
            {
                levelSelector.NextImage();
            }
            else if (InputHandler.IsGamepadButtonPressed(Microsoft.Xna.Framework.Input.Buttons.LeftShoulder))
            {
                levelSelector.PreviousImage();
            }
            base.Update(_time);
        }
    }
}
